/****************************************************************
 * Author: Benedikt Niedermayr  <benbrenson@gmx.de>
 *
 * Description: This file contains the structure of the hardware
 * system. It specifies the direct hardware acces and mapps them
 * as functions to the protokoll_handlers.
 * If you want to connect new Hardware to your system, then you
 * have to add it here.
 */

#ifndef __HARDWARE_H
#define __HARDWARE_H

#include "temp_controll.h"




/* Define the systems Hardware */

/* sensors  
 * Temperatur sensors: LM75
 * Humidity sensors:
 */
#define SET_SENSOR(snum, slocation, stype, scritical_max, scritical_min, scrit_reach, sget_val) \
	{									\
		.num = snum, 					\
		.location = slocation, 			\
		.type = stype, 					\
		.critical_max = scritical_max,  \
		.critical_min = scritical_min,  \
	    .crit_reach = scrit_reach,      \
		.get_val = sget_val			    \
	}



/********************************
 * Please fill in your hardware *
 ********************************/  
int ds18b20_getval (int pnum);

#define NUM_SENSORS 	6 //<--edit here!
#define FIRST_SENSOR 	1
#define LAST_SENSOR		(FIRST_SENSOR + NUM_SENSORS - 1)
struct sensor sensors_terr1[] = {
	SET_SENSOR(1, "sonne", SENS_HEAT, 50, 30, 0, ds18b20_getval),
	SET_SENSOR(2, "kalt",  SENS_COLD, 30, 10, 0, ds18b20_getval),
	SET_SENSOR(3, "oben",  SENS_STD, 45, 10, 0, ds18b20_getval),
	SET_SENSOR(4, "unten",  SENS_STD, 45, 10, 0, ds18b20_getval),
	SET_SENSOR(5, "aussen",  SENS_OUT, 30, 10, 0, ds18b20_getval),
	SET_SENSOR(6, "feucht",  SENS_HUMIDITY, 70, 5, 0, ds18b20_getval),
	/* Here you can enter the data for each sensor. You must already have
	   writen a hardware acess function and mapp it here after that*/

};


/** 
 * get-val for ds18b20 Temperatur sensors. Hardware Access function
 * for one-wire sensors.
 * @pnum: This identifies the correct sensor
 */
int ds18b20_getval (int pnum)
{
	FILE *fd;
	char buffer[BUFF_SIZE];
	
	if(pnum == 1)
	{
		if((fd = fopen("/sys/bus/w1/devices/28-000004509084/w1_slave", "r")) == NULL)
		{
			puts("Error while opening file");
			return __ERRSENS;
		}	
	}
	else if(pnum == 2)
	{
		if((fd = fopen("/sys/bus/w1/devices/28-00000450a152/w1_slave", "r")) == NULL)
		{
			puts("Error while opening file");
			return __ERRSENS;
		}
	}
	else if(pnum == 3)
	{
			
		if((fd = fopen("/sys/bus/w1/devices/28-00000450adf4/w1_slave", "r")) == NULL)
		{
			puts("Error while opening file");
			return __ERRSENS;
		}

	}
	else if(pnum == 4)
	{
		if((fd = fopen("/sys/bus/w1/devices/28-00000450b2a6/w1_slave", "r")) == NULL)
		{
			puts("Error while opening file");
			return __ERRSENS;
		}
	}
	else if(pnum == 5)
	{
		if((fd = fopen("/sys/bus/w1/devices/28-00000450f733/w1_slave", "r")) == NULL)
		{
			puts("Error while opening file: ");
			return __ERRSENS;
		}
	}
	else if(pnum == 6)
	{
		if((fd = fopen("/sys/class/hwmon/hwmon6/device/humidity1_input", "r")) == NULL)
		{
			puts("Error while opening file: ");
			return __ERRSENS;
		}
	}

	//Decode Temperature Value
	fgets(buffer, 10, fd);
	fclose(fd);
	return (atoi(buffer)/1000);
}



/*
 * Outputs 
 * The following defines the outputs of this system.
 * Have a look at temp_controll.h for definitions.
 */
#define SET_OUTPUT(snum, stype, slocation, sset_state,sforce_state)\
	{											\
		.num = snum,							\
		.type = stype,							\
		.location = slocation,					\
		.set_state = sset_state,				\
		.force_state = sforce_state				\
	}											


/********************************
 * Please fill in your hardware *
 ********************************/
int sunlight_set_state(int num, int state);
int fan_set_speed(int num, int speed);

#define NUM_OUTPUTS  3 //<--edit here!
#define FIRST_OUTPUT 1
#define LAST_OUTPUT	 (FIRST_OUTPUT + NUM_OUTPUTS - 1)
struct output outputs_terr1[] = {
	SET_OUTPUT(1, OUT_GROUND, "Grundbeleuchtung",sunlight_set_state,1),
	SET_OUTPUT(2, OUT_HEAT_HIGH, "Sonnenbeleuchtung",sunlight_set_state,1),
    SET_OUTPUT(3, OUT_FAN, "Belueftung", fan_set_speed,1),
};



int sunlight_set_state (int num, int state)
{
	return 0;
}


int fan_set_speed(int num, int speed)
{
	return 0;
}






/**
 * Terrarium 1
 */
struct terra terra1 = {
	.sensor_p  = sensors_terr1,
	.sensor_pf = sensors_terr1,
	.output_p = outputs_terr1,
	.output_pf = outputs_terr1,
	.num = 1,
};



#endif

